package com.indextank.roo.addon;

import java.util.logging.Logger;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.osgi.service.component.ComponentContext;
import org.springframework.roo.model.JavaType;
import org.springframework.roo.shell.CliAvailabilityIndicator;
import org.springframework.roo.shell.CliCommand;
import org.springframework.roo.shell.CliOption;
import org.springframework.roo.shell.CommandMarker;
import org.springframework.roo.shell.converters.StaticFieldConverter;

/**
 * Example of a command class. The command class is registered by the Roo shell following an
 * automatic classpath scan. You can provide simple user presentation-related logic in this
 * class. You can return any objects from each method, or use the logger directly if you'd
 * like to emit messages of different severity (and therefore different colors on 
 * non-Windows systems).
 * 
 * @since 1.1.1
 */
@Component // Use these Apache Felix annotations to register your commands class in the Roo container
@Service
public class IndextankCommands implements CommandMarker { // All command types must implement the CommandMarker interface
	
	/**
	 * Get hold of a JDK Logger
	 */
	private Logger log = Logger.getLogger(getClass().getName());

	/**
	 * Get a reference to the IndextankOperations from the underlying OSGi container
	 */
	@Reference private IndextankOperations operations; 
	
	/**
	 * This method is optional. It allows automatic command hiding in situations when the command should not be visible.
	 * For example the 'entity' command will not be made available before the user has defined his persistence settings 
	 * in the Roo shell or directly in the project.
	 * 
	 * You can define multiple methods annotated with {@link CliAvailabilityIndicator} if your commands have differing
	 * visibility requirements.
	 * 
	 * @return true (default) if the command should be visible at this stage, false otherwise
	 */
	@CliAvailabilityIndicator({ "indextank setup" } )
	public boolean isSetupCommandAvailable() {
		return operations.isInstallSearchAvailable();
	}


    @CliAvailabilityIndicator({"indextank add", "indextank all"})
    public boolean isIndextankCommandAvailable() {
        return operations.isSearchAvailable();
    }

	
	/**
	 * @param apiUrl
	 * @param indexName
	 */
	@CliCommand(value = "indextank setup", help = "Install support for IndexTank search integration")
	public void setup(
		@CliOption(key = "apiUrl", mandatory = true, help = "indextank api url") String apiUrl,
		@CliOption(key = "indexName", mandatory = true, help = "indextank index name") String indexName) {
		operations.setupConfig(apiUrl, indexName);
	}
	

    /**
     * @param javaType
     */
    public void setup(
        @CliOption(key = "class", mandatory = true, help = "the target type made searchable", optionContext="update,project") JavaType javaType ) {


        operations.addSearch(javaType);
    }


    @CliCommand(value = "indextank all", help = "Make all eligible project types searchable")
    public void setup() {
        operations.addAll();
    }
}
